module gwindump.mainform;

import std.thread;

import dfl.all;

import windump.scanning.scanner;
import windump.config;
import windump.common;

import gwindump.aboutform;

class MainForm: dfl.form.Form
{
	// Do not modify or move this block of variables.
	//~Entice Designer variables begin here.
	private dfl.listview.ListView m_listView;
    private StatusBar m_status;
	//~Entice Designer variables end here.
	
	
	this()
	{
		initializeMainForm();
        initializeStatusBar();
		initializeMainMenu();
		
		//@  Other MainForm initialization code here.
	}	
	
	private void initializeMainForm()
	{
		// Do not manually modify this function.
		//~Entice Designer 0.8.5.02 code begins here.
		//~DFL Form
		text = "Malt Windump 密码扫描程序";
		clientSize = dfl.all.Size(488, 273);
		//~DFL dfl.listview.ListView=m_listView
		m_listView = new dfl.listview.ListView();
		m_listView.name = "listView";
		m_listView.dock = dfl.all.DockStyle.FILL;
		m_listView.fullRowSelect = true;
		m_listView.multiSelect = false;
		m_listView.view = dfl.all.View.DETAILS;
		m_listView.bounds = dfl.all.Rect(0, 0, 488, 273);
		m_listView.parent = this;
		//~Entice Designer 0.8.5.02 code ends here.
		
				
		//Add headers
		ColumnHeader ch;
		
		ch = new ColumnHeader();
		ch.text = "提供者";
		ch.width = 200;
		m_listView.columns.add(ch);

		ch = new ColumnHeader();
		ch.text = "主机";
		ch.width = 100;
		m_listView.columns.add(ch);
		
		ch = new ColumnHeader();
		ch.text = "帐号";
		ch.width = 60;
		m_listView.columns.add(ch);
		
		ch = new ColumnHeader();
		ch.text = "密码";
		ch.width = 60;
		m_listView.columns.add(ch);	
		
		}

    //初始化主菜单
    private void initializeMainMenu()
    {
        // Create main menu
		MainMenu mm;
		MenuItem mpop, mi;
		
		mm = new MainMenu;
		
		with(mpop = new MenuItem)
		{
			text = "文件(&F)";
			index = 0;
			mm.menuItems.add(mpop);
		}
		
		with(mi = new MenuItem)
		{
			text = "退出(&X)";
			index = 0;
			click ~= &menuFileExit;
			mpop.menuItems.add(mi);
		}
		//addShortcut(Keys.CONTROL | Keys.N, &fileNew);	

		with(mpop = new MenuItem)
		{
			text = "查看(&V)";
			index = 1;
			mm.menuItems.add(mpop);
		}
		
		with(mi = new MenuItem)
		{
			text = "刷新(&R)\tF5";
			index = 0;
			click ~= &menuViewRefresh;
			mpop.menuItems.add(mi);
		}
		addShortcut(Keys.F5, &menuViewRefresh);	
		
		with(mpop = new MenuItem)
		{
			text = "帮助(&H)";
			index = 2;
			mm.menuItems.add(mpop);
		}
		
		with(mi = new MenuItem)
		{
			text = "关于(&A)...\tF1";
			index = 0;
			click ~= &menuHelpAbout;
			mpop.menuItems.add(mi);
		}
		addShortcut(Keys.F1, &menuHelpAbout);	
		
		this.menu = mm;
    }

    //初始化状态栏
    private void initializeStatusBar()
    {
        m_status = new StatusBar();
		m_status.name = "status";
		m_status.showPanels = true;
		m_status.dock = dfl.control.DockStyle.BOTTOM;
		m_status.bounds = Rect(0, 250, 292, 23);
		m_status.parent = this;
    }
	
	//Event Handlers
	private void menuFileExit(Object sender, EventArgs args)
	{
		Application.exit();
	}
	
	private void menuHelpAbout(Object sender, EventArgs args)
	{
	    auto af = new AboutForm();
        af.showDialog();
	}
	
	private void menuViewRefresh(Object sender, EventArgs args)
	{
        doScan();
	}

    private void doScan()
    {
        Thread thread = new Thread(delegate() {
            auto results = Scanner.instance.doScan();

            this.m_listView.items.clear();

            foreach(ri; results)
            {
                auto lvi = new ListViewItem();
                lvi.text = ri.provider;
                lvi.subItems.add(ri.host);
                lvi.subItems.add(ri.account);
                lvi.subItems.add(ri.password);
                m_listView.items.add(lvi);
            }
            return 0;
        });

        thread.run();
        thread.wait();
    }
}


int main()
{
	int result = 0;
	
	try
	{
		//Application.enableVisualStyles();
		
		//@  Other application initialization code here.
		
		Application.run(new MainForm());
	}
	catch(Object o)
	{
		msgBox(o.toString(), "错误", MsgBoxButtons.OK, MsgBoxIcon.ERROR);
		
		result = 1;
	}
	
	return result;
}

